package edu.uwlax.cs.oayonlinestore.client.gui.admin;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

import edu.uwlax.cs.oayonlinestore.client.OnlinestoreRService;
import edu.uwlax.cs.oayonlinestore.client.gui.Store;
import edu.uwlax.cs.oayonlinestore.client.gui.admin.PageAdminEditCategory.DoneAddAsycHandler;
import edu.uwlax.cs.oayonlinestore.client.gui.admin.PageAdminEditCategory.DoneRemoveAsycHandler;
import edu.uwlax.cs.oayonlinestore.client.utils.DefaultAsyncCallback;
import edu.uwlax.cs.oayonlinestore.client.utils.DekoTable;
import edu.uwlax.cs.oayonlinestore.client.utils.SwitchPage;
import edu.uwlax.cs.oayonlinestore.client.utils.Title;
import edu.uwlax.cs.oayonlinestore.client.utils.TitlePage;
import edu.uwlax.cs.oayonlinestore.vo.CategoryVO;
import edu.uwlax.cs.oayonlinestore.vo.UserDetailsVO;
import edu.uwlax.cs.oayonlinestore.vo.UserVO;

public class PageAdminListAccounts extends TitlePage {

	public class AccountTable extends DekoTable {
		Button delete;
		public AccountTable() {
			super();
			String anchor = Store.getFrame().getAdminEditAcc().getAnchor(0);
			Button add = new Button("Add New User");
			add.addClickListener(new SwitchPage(anchor));
			addTitle("Login");
			addTitle("First Name");
			addTitle("Last Name");
			addTitle("");
			addTitleWidget(add);
		}

		public void addItem(UserDetailsVO u) {
			String anchor = Store.getFrame().getAdminEditAcc().getAnchor(u.getId());
			Hyperlink edit = new Hyperlink("Edit", anchor);
			AddRow r = addRow();
			r.add(u.getLogin());
			r.add(u.getFirstname());
			r.add(u.getLastname());
			r.add(edit);
			r.add(delete = new Button("Delete"));
			delete.addClickListener(new ButtonClickListener (u));
		}
		
		public class ButtonClickListener implements ClickListener {
			UserDetailsVO vo = null;

			public ButtonClickListener(UserDetailsVO u) {
					vo = u;
			}

			public void onClick(Widget arg0) {

					OnlinestoreRService.Util.getInstance().setUserInactive(vo.getId(),new DoneAddAsycHandler());

			}
		}
		
		public class DoneAddAsycHandler extends DefaultAsyncCallback {
		
			public void onSuccess(Object o) {

				//Window.alert("User has been approved");
				onShow();
			}
		}
	}
	
	AccountTable t;

	protected void create() {

		//VerticalPanel p = new VerticalPanel();
		setTitle("Administrate Accounts");
		//add(new Title("Administrate Accounts"));
		add(t = new AccountTable());
		//setWidget(p);

	}
	protected void onShow() {

		OnlinestoreRService.Util.getInstance().getUserDetailList(getAsyncHandler());
	}

	protected void onDataArrived(Object o) {

		t.cleanExceptHeader();
		UserDetailsVO[] users = (UserDetailsVO[]) o;
		// Window.alert(users.length +"");
		for (int i = 0; i < users.length; i++)
			t.addItem(users[i]);

	}


	public String getID() {
		return "AdminAccounts";
	}

}
